<?php

namespace Controller;

use Handler\LobbyHandler;
use Handler\UserHandler;
use Handler\PushHandler;
use Handler\SFSHandler;
use Extension\Tools;

class WebController extends BaseController {
	
	// -------------------------------GM-------------------------------//
	
	/**
	 * 更新安装包信息
	 *
	 * @param unknown $clientId        	
	 * @param array $message        	
	 */
	public function mb_UpdateInstallPackageInfo($clientId, $message) {
		$id = $message ['id'];
		if ($id == 1) {
			LobbyHandler::mb_GetInstance ()->mb_UpdateApkInfo ();
		} else if ($id == 2) {
			LobbyHandler::mb_GetInstance ()->mb_UpdateIpaInfo ();
		} else if ($id == 3) {
			LobbyHandler::mb_GetInstance ()->mb_InitLobbyInfo ();
		}
		$this->mb_OutWebMessage ( $clientId, '更新安装包信息完成' );
	}
	/**
	 * 获取服务器简单状态
	 *
	 * @param unknown $clientId        	
	 * @param array $message        	
	 */
	public function mb_GetServerStatus($clientId, $message) {
		$data = UserHandler::mb_GetInstance ()->mb_GetUserStatus ();
		$curUserCount = $data ['cur'];
		$maxUserCount = $data ['max'];
		$message = "+---------------------------简讯---------------------------+<br>" . "当前在线人数：$curUserCount <br>" . "历史最高人数：$maxUserCount <br>" . "+---------------------------END---------------------------+<br>";
		$this->mb_OutWebMessage ( $clientId, $message );
	}
	/**
	 * 进入维护状态
	 *
	 * @param unknown $clientId        	
	 * @param array $message        	
	 */
	public function mb_EnterMaintenance($clientId, $message) {
		LobbyHandler::mb_GetInstance ()->mb_SetLobbyStatus ( 1 );
		PushHandler::mb_GetInstance ()->mb_EnterMaintenancePush ();
		$this->mb_OutWebMessage ( $clientId, '设置成功，并已通知所有玩家下线' );
	}
	/**
	 * 退出维护状态
	 *
	 * @param unknown $clientId        	
	 * @param array $message        	
	 */
	public function mb_ExitMaintenance($clientId, $message) {
		LobbyHandler::mb_GetInstance ()->mb_SetLobbyStatus ( 0 );
		$this->mb_OutWebMessage ( $clientId, '设置成功' );
	}
	/**
	 * 更新资源包版本号
	 *
	 * @param unknown $clientId        	
	 * @param array $message        	
	 */
	public function mb_UpdateAppInfo($clientId, $message) {
		LobbyHandler::mb_GetInstance ()->mb_InitAppInfo ();
		$this->mb_OutWebMessage ( $clientId, '更新资源包版本号成功' );
	}
	/**
	 * http://127.0.0.1:1007?zone=Web&method=mb_TestMonth
	 * @param unknown $clientId
	 * @param unknown $message
	 */
	public function mb_TestMonth($clientId, $message){
		LobbyHandler::mb_GetInstance()->mb_TestMonth();
		$this->mb_OutWebMessage ( $clientId, '成功' );
	}
	// -------------------------------后台-------------------------------//
	/**
	 * 更新用户数据
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_UpdateUserData($clientId, $message) {
		$name = $message ['name'];
		$user = UserHandler::mb_GetInstance ()->mb_GetUserForName ( $name );
		if (isset ( $user )) {
			UserHandler::mb_GetInstance ()->mb_UpdateUserDataFromSFS ( $user );
			PushHandler::mb_GetInstance ()->mb_UserDataPush ( $user );
		}
		$this->mb_OutWebMessage ( $clientId, '通知成功' );
	}
	/**
	 * 更新大厅公告，滑动公告，充值公告信息
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_UpdateLobbyMessages($clientId, $message) {
		$key = $message ['key'];
		$value = $message ['value'];
		
		LobbyHandler::mb_GetInstance ()->mb_UpdateLobbyInfo ( $key, $value );
		PushHandler::mb_GetInstance ()->mb_UpdateLobbyInfoPush ();
		$this->mb_OutWebMessage ( $clientId, '操作成功' );
	}
	/**
	 * 更新大厅信息
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_UpdateLobbInfo($clientId, $message) {
		$status = $message ['status'];
		$regist = $message ['sw_regist'];
		$login = $message ['sw_login'];
		$phone = $message ['sw_phone'];
		
		$bulletin = $message ['bulletin'];
		$broadcast = $message ['broadcast'];
		$recharge = $message ['recharge'];
		
		LobbyHandler::mb_GetInstance ()->mb_UpdateLobbyInfo ( 'status', ( int ) $status );
		LobbyHandler::mb_GetInstance ()->mb_UpdateLobbyInfo ( 'sw_regist', $regist );
		LobbyHandler::mb_GetInstance ()->mb_UpdateLobbyInfo ( 'sw_login', $login );
		LobbyHandler::mb_GetInstance ()->mb_UpdateLobbyInfo ( 'sw_phone', $phone );
		LobbyHandler::mb_GetInstance ()->mb_UpdateLobbyInfo ( 'bulletin', $bulletin );
		LobbyHandler::mb_GetInstance ()->mb_UpdateLobbyInfo ( 'broadcast', $broadcast );
		LobbyHandler::mb_GetInstance ()->mb_UpdateLobbyInfo ( 'recharge', $recharge );
		$this->mb_OutWebMessage ( $clientId, '操作成功' );
	}
	/**
	 * 更改游戏状态
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_ChangeAppStatus($clientId, $message) {
		$gameId = $message ['appid'];
		$status = $message ['status'];
		// LobbyHandler::mb_GetInstance ()->mb_ChangeAppStatus ( $gameId, $status );
		LobbyHandler::mb_GetInstance ()->mb_InitAppInfo ();
		PushHandler::mb_GetInstance ()->mb_UpdateAppInfoPush ();
		$this->mb_OutWebMessage ( $clientId, '操作成功' );
	}
	/**
	 * 更改游戏币值
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_ChangeAppCoinValue($clientId, $message) {
		$gameId = $message ['appid'];
		$coinValue = $message ['coinvalue'];
		
		Tools::mb_LogResult ( "gameid:$gameId -- coinValue:$coinValue" );
		
		LobbyHandler::mb_GetInstance ()->mb_ChangeAppCoinValue ( $gameId, $coinValue );
		PushHandler::mb_GetInstance ()->mb_UpdateAppInfoPush ();
		$this->mb_OutWebMessage ( $clientId, '操作成功' );
	}
	/**
	 * 更改游戏难度
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_ChangeAppLevel($clientId, $message) {
		$gameId = $message ['appid'];
		$level = $message ['level'];
		
		Tools::mb_LogResult ( "gameid:$gameId -- level:$level" );
		
		LobbyHandler::mb_GetInstance ()->mb_ChangeAppLevel ( $gameId, $level );
		PushHandler::mb_GetInstance ()->mb_UpdateAppInfoPush ();
		$this->mb_OutWebMessage ( $clientId, '操作成功' );
	}
	/**
	 * 强制下线
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_ForcedOffline($clientId, $message) {
		$name = $message ['name'];
		$user = UserHandler::mb_GetInstance ()->mb_GetUserForName ( $name );
		if (isset ( $user ) == false) {
			$this->mb_OutWebMessage ( $clientId, '该玩家不在线' );
			return;
		}
		PushHandler::mb_GetInstance ()->mb_UserForcedOffline ( $user );
		$this->mb_OutWebMessage ( $clientId, '强制下线成功' );
	}
	/**
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_UpdateUserPass($clientId, $message) {
		$name = $message ['name'];
		$pass = $message ['pass'];
		$user = UserHandler::mb_GetInstance ()->mb_GetUserForName ( $name );
		if (isset ( $user )) {
			$user->m_BasePass = $pass;
			$user->m_EncPass = md5 ( $basePass );
		} else {
			UserHandler::mb_GetInstance ()->mb_UpdateUserDataToDB ( $name, array (
					'basepass' => $pass,
					'encpass' => md5 ( $pass ) 
			) );
		}
		$this->mb_OutWebMessage ( $clientId, '保存成功' );
	}
	/**
	 * 获取玩家游戏状态
	 * 
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_GetUserGameStatus($clientId, $message) {
		$name = $message ['name'];
		$user = UserHandler::mb_GetInstance ()->mb_GetUserForName ( $name );
		if ($user == null) {
			$this->mb_OutWebMessage ( $clientId, '-1' );
		} else {
			$this->mb_OutWebMessage ( $clientId, $user->m_GameId );
		}
	}
	// ////////////////////////////////////////////////////////////////////////////////
	public function mb_GetOnlineUsers($clientId, $message) {
		$users = UserHandler::mb_GetInstance ()->mb_GetAllUsers ();
		// $arr = '';
		// if (isset ( $users )) {
		// foreach ( $users as $user ) {
		// $arr .= $user->m_Id . '|';
		// }
		// }
		// $arr = substr ( $arr, 0, strlen ( $arr ) - 1 );
		// $this->mb_OutWebMessage ( $clientId, $arr);
		$arr = array ();
		if (isset ( $users )) {
			foreach ( $users as $user ) {
				$arr [] = $user->m_Id;
			}
		}
		$this->mb_OutWebMessage ( $clientId, json_encode ( $arr ) );
	}
	/**
	 * 获取各个游戏在线用户数
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_GetGameOnlineUsers($clientId, $message) {
		$this->mb_OutWebMessage ( $clientId, json_encode ( LobbyHandler::mb_GetInstance ()->mb_GetGameOnLineUsers () ) );
	}
	/**
	 * 获取对应游戏id的详细在线用户数
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_GetGameOnlineUsersDetail($clientId, $message) {
		$gameId = $message ['id'];
		$this->mb_OutWebMessage ( $clientId, json_encode ( LobbyHandler::mb_GetInstance ()->mb_GetGameOnLineUserForGameId ( $gameId ) ) );
	}
	
	/**
	 * 更新用户充值记录
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_UpdateDataRecord($clientId, $message) {
		$name = $message ['name'];
		$type = $message ['type']; // 记录类型（0：充值记录，1：消耗记录）
		$user = UserHandler::mb_GetInstance ()->mb_GetUserForName ( $name );
		if (isset ( $user )) {
			UserHandler::mb_GetInstance ()->mb_UpdateUserDataFromSFS ( $user );
			PushHandler::mb_GetInstance ()->mb_UserDataPush ( $user );
			PushHandler::mb_GetInstance ()->mb_UpdateDataRecordPush ( $user, $type );
		}
		$this->mb_OutWebMessage ( $clientId, '通知成功' );
	}
}